﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace sharpCmd.Windows.Forms
{
    class ProgressBarWithText : ProgressBar
    {
        public ProgressBarWithText()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            fmtBlack = new StringFormat();
            fmtBlack.Alignment = StringAlignment.Center;
            fmtWhite = new StringFormat();
            fmtWhite.FormatFlags = StringFormatFlags.NoWrap;
            fmtWhite.Trimming = StringTrimming.None;
        }

        private StringFormat fmtBlack;
        private StringFormat fmtWhite;
        
        public float Percent
        {
            get
            {
                return (float)Value / (float)Maximum * 100f;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            float barRight = ((Percent / 100f) * (float)Width);
            string percentString = string.Format("0.0%", Percent);
            float stringWidth = e.Graphics.MeasureString(percentString, this.Font).Width;
            e.Graphics.DrawString(percentString, this.Font, Brushes.Black, new RectangleF(0, 0, this.Width, this.Height), fmtBlack);
            e.Graphics.FillRectangle(SystemBrushes.Highlight, 0, 0, barRight, this.Height);
            float startX = (this.Width - stringWidth) / 2;
            float rectWidth = barRight - startX;
            e.Graphics.DrawString(percentString, this.Font, Brushes.White, new RectangleF(startX, 0, rectWidth, this.Height), fmtWhite);
        }
    }
}